From d8a4248e024d6f46529fbcfc950d67052826d03f Mon Sep 17 00:00:00 2001 From: "emellor@leeni.uk.xensource.com" Date: Mon, 27 Feb 2006 16:13:05 +0100 Subject: [PATCH] The attached patch does the following: - introduces two basic tests for the virtual TPM (skipped on hardware where a TPM is not available or vtpm manager has not been started manually before) - adapts the Makefiles to make the test cases available - splits up the hotplug files into two files where the vtpm-common.sh can be sourced by 'other' scripts as well - uses echo "$var" to return variables from functions - gets rid of the 'set +e" work-around - introduces a script (vtpm-delete) to delete entries from the virtual TPM directory in /etc/xen/vtpm.db The xm-tests will skip over the 2nd test on most systems and the patch has otherwise no effect on existing tests. Signed-off-by: Stefan Berger --- tools/examples/Makefile | 4 +- tools/examples/vtpm | 2 +- tools/examples/vtpm-common.sh | 111 +++++++++--------- tools/xm-test/configure.ac | 1 + tools/xm-test/lib/XmTestLib/XenDomain.py | 1 + tools/xm-test/tests/Makefile.am | 1 + xen-unstable.hg/tools/examples/vtpm-delete | 9 ++ .../tools/examples/vtpm-hotplug-common.sh | 35 ++++++ .../xm-test/tests/vtpm/01_vtpm-list_pos.py | 45 +++++++ .../xm-test/tests/vtpm/02_vtpm-cat_pcrs.py | 81 +++++++++++++ .../tools/xm-test/tests/vtpm/Makefile.am | 22 ++++ 11 files changed, 256 insertions(+), 56 deletions(-) create mode 100644 xen-unstable.hg/tools/examples/vtpm-delete create mode 100644 xen-unstable.hg/tools/examples/vtpm-hotplug-common.sh create mode 100644 xen-unstable.hg/tools/xm-test/tests/vtpm/01_vtpm-list_pos.py create mode 100644 xen-unstable.hg/tools/xm-test/tests/vtpm/02_vtpm-cat_pcrs.py create mode 100644 xen-unstable.hg/tools/xm-test/tests/vtpm/Makefile.am diff --git a/tools/examples/Makefile b/tools/examples/Makefile index 1e47ebbea1..8ffd794972 100644 --- a/tools/examples/Makefile +++ b/tools/examples/Makefile @@ -26,10 +26,10 @@ XEN_SCRIPTS += network-route vif-route XEN_SCRIPTS += network-nat vif-nat XEN_SCRIPTS += block XEN_SCRIPTS += block-enbd block-nbd -XEN_SCRIPTS += vtpm +XEN_SCRIPTS += vtpm vtpm-delete XEN_SCRIPT_DATA = xen-script-common.sh locking.sh logging.sh XEN_SCRIPT_DATA += xen-hotplug-common.sh xen-network-common.sh vif-common.sh -XEN_SCRIPT_DATA += block-common.sh vtpm-common.sh +XEN_SCRIPT_DATA += block-common.sh vtpm-common.sh vtpm-hotplug-common.sh XEN_HOTPLUG_DIR = /etc/hotplug XEN_HOTPLUG_SCRIPTS = xen-backend.agent diff --git a/tools/examples/vtpm b/tools/examples/vtpm index 553c8a0904..09bb7c71e1 100644 --- a/tools/examples/vtpm +++ b/tools/examples/vtpm @@ -1,7 +1,7 @@ #!/bin/sh dir=$(dirname "$0") -. "$dir/vtpm-common.sh" +. "$dir/vtpm-hotplug-common.sh" vtpm_fatal_error=0 diff --git a/tools/examples/vtpm-common.sh b/tools/examples/vtpm-common.sh index 72c16abd9e..190f74193a 100644 --- a/tools/examples/vtpm-common.sh +++ b/tools/examples/vtpm-common.sh @@ -17,21 +17,8 @@ # dir=$(dirname "$0") -. "$dir/xen-hotplug-common.sh" - -findCommand "$@" -if [ "$command" != "online" ] && - [ "$command" != "offline" ] && - [ "$command" != "add" ] && - [ "$command" != "remove" ] -then - log err "Invalid command: $command" - exit 1 -fi - - -XENBUS_PATH="${XENBUS_PATH:?}" - +. "$dir/logging.sh" +. "$dir/locking.sh" VTPMDB="/etc/xen/vtpm.db" @@ -58,15 +45,19 @@ if [ -z "$VTPM_IMPL_DEFINED" ]; then function vtpm_resume() { true } + function vtpm_delete() { + true + } fi + #Find the instance number for the vtpm given the name of the domain # Parameters # - vmname : the name of the vm # Return value # Returns '0' if instance number could not be found, otherwise # it returns the instance number in the variable 'instance' -function find_instance () { +function vtpmdb_find_instance () { local vmname=$1 local ret=0 instance=`cat $VTPMDB | \ @@ -80,18 +71,17 @@ function find_instance () { } \ }'` if [ "$instance" != "" ]; then - ret=1 + ret=$instance fi - return $ret + echo "$ret" } # Check whether a particular instance number is still available -# returns '1' if it is available -function is_free_instancenum () { +# returns "0" if it is not available, "1" otherwise. +function vtpmdb_is_free_instancenum () { local instance=$1 local avail=1 - #Allowed instance number range: 1-255 if [ $instance -eq 0 -o $instance -gt 255 ]; then avail=0 @@ -110,13 +100,13 @@ function is_free_instancenum () { fi done fi - return $avail + echo "$avail" } # Get an available instance number given the database # Returns an unused instance number -function get_free_instancenum () { +function vtpmdb_get_free_instancenum () { local ctr local instances local don @@ -145,12 +135,12 @@ function get_free_instancenum () { fi let ctr=ctr+1 done - let instance=$ctr + echo "$ctr" } # Add a domain name and instance number to the DB file -function add_instance () { +function vtpmdb_add_instance () { local vmname=$1 local inst=$2 @@ -159,8 +149,8 @@ function add_instance () { echo "#1st column: domain name" >> $VTPMDB echo "#2nd column: TPM instance number" >> $VTPMDB fi - validate_entry $vmname $inst - if [ $? -eq 0 ]; then + res=$(vtpmdb_validate_entry $vmname $inst) + if [ $res -eq 0 ]; then echo "$vmname $inst" >> $VTPMDB fi } @@ -168,11 +158,10 @@ function add_instance () { #Validate whether an entry is the same as passed to this #function -function validate_entry () { +function vtpmdb_validate_entry () { local rc=0 local vmname=$1 local inst=$2 - local res res=`cat $VTPMDB | \ gawk -vvmname=$vmname \ @@ -197,13 +186,15 @@ function validate_entry () { elif [ "$res" == "2" ]; then let rc=2 fi - return $rc + echo "$rc" } #Remove an entry from the vTPM database given its domain name -function remove_entry () { +#and instance number +function vtpmdb_remove_entry () { local vmname=$1 + local instance=$2 local VTPMDB_TMP="$VTPMDB".tmp `cat $VTPMDB | \ gawk -vvmname=$vmname \ @@ -214,6 +205,7 @@ function remove_entry () { '} > $VTPMDB_TMP` if [ -e $VTPMDB_TMP ]; then mv -f $VTPMDB_TMP $VTPMDB + vtpm_delete $instance else log err "Error creating temporary file '$VTPMDB_TMP'." fi @@ -222,7 +214,7 @@ function remove_entry () { # Find the reason for the creation of this device: # Set global REASON variable to 'resume' or 'create' -function get_create_reason () { +function vtpm_get_create_reason () { local resume=$(xenstore-read $XENBUS_PATH/resume) if [ "$resume" == "True" ]; then REASON="resume" @@ -231,32 +223,30 @@ function get_create_reason () { fi } + #Create a vTPM instance # If no entry in the TPM database is found, the instance is # created and an entry added to the database. function vtpm_create_instance () { local domname=$(xenstore_read "$XENBUS_PATH"/domain) local res - set +e - get_create_reason + local instance + vtpm_get_create_reason claim_lock vtpmdb - - find_instance $domname - res=$? - if [ $res -eq 0 ]; then + instance=$(vtpmdb_find_instance $domname) + if [ "$instance" == "0" ]; then #Try to give the preferred instance to the domain instance=$(xenstore_read "$XENBUS_PATH"/pref_instance) if [ "$instance" != "" ]; then - is_free_instancenum $instance - res=$? + res=$(vtpmdb_is_free_instancenum $instance) if [ $res -eq 0 ]; then - get_free_instancenum + instance=$(vtpmdb_get_free_instancenum) fi else - get_free_instancenum + instance=$(vtpmdb_get_free_instancenum) fi - add_instance $domname $instance + vtpmdb_add_instance $domname $instance if [ "$REASON" == "create" ]; then vtpm_create $instance elif [ "$REASON" == "resume" ]; then @@ -279,25 +269,40 @@ function vtpm_create_instance () { true fi xenstore_write $XENBUS_PATH/instance $instance - set -e } -#Remove an instance +#Remove an instance when a VM is terminating or suspending. +#Since it is assumed that the VM will appear again, the +#entry is kept in the VTPMDB file. function vtpm_remove_instance () { local domname=$(xenstore_read "$XENBUS_PATH"/domain) - set +e - find_instance $domname - res=$? - if [ $res -eq 0 ]; then - #Something is really wrong with the DB - log err "vTPM DB file $VTPMDB has no entry for '$domname'" - else + + claim_lock vtpmdb + + instance=$(vtpmdb_find_instance $domname) + + if [ "$instance" != "0" ]; then if [ "$REASON" == "suspend" ]; then vtpm_suspend $instance fi fi - set -e + + release_lock vtpmdb } +#Remove an entry in the VTPMDB file given the domain's name +#1st parameter: The name of the domain +function vtpm_delete_instance () { + local rc + + claim_lock vtpmdb + + instance=$(vtpmdb_find_instance $1) + if [ "$instance" != "0" ]; then + vtpmdb_remove_entry $1 $instance + fi + + release_lock vtpmdb +} diff --git a/tools/xm-test/configure.ac b/tools/xm-test/configure.ac index 22b2addc8f..c2e380cfe1 100644 --- a/tools/xm-test/configure.ac +++ b/tools/xm-test/configure.ac @@ -93,6 +93,7 @@ AC_CONFIG_FILES([ tests/unpause/Makefile tests/vcpu-pin/Makefile tests/vcpu-disable/Makefile + tests/vtpm/Makefile tests/enforce_dom0_cpus/Makefile lib/XmTestReport/xmtest.py lib/XmTestLib/config.py diff --git a/tools/xm-test/lib/XmTestLib/XenDomain.py b/tools/xm-test/lib/XmTestLib/XenDomain.py index 80825064a8..6ffb4ffab7 100644 --- a/tools/xm-test/lib/XmTestLib/XenDomain.py +++ b/tools/xm-test/lib/XmTestLib/XenDomain.py @@ -99,6 +99,7 @@ class XenConfig: # These options need to be lists self.defaultOpts["disk"] = [] self.defaultOpts["vif"] = [] + self.defaultOpts["vtpm"] = [] self.opts = self.defaultOpts diff --git a/tools/xm-test/tests/Makefile.am b/tools/xm-test/tests/Makefile.am index 35e85752a4..be1038a806 100644 --- a/tools/xm-test/tests/Makefile.am +++ b/tools/xm-test/tests/Makefile.am @@ -23,6 +23,7 @@ SUBDIRS = \ unpause \ vcpu-disable \ vcpu-pin \ + vtpm \ enforce_dom0_cpus \ save restore migrate diff --git a/xen-unstable.hg/tools/examples/vtpm-delete b/xen-unstable.hg/tools/examples/vtpm-delete new file mode 100644 index 0000000000..14bfddcd94 --- /dev/null +++ b/xen-unstable.hg/tools/examples/vtpm-delete @@ -0,0 +1,9 @@ +#!/bin/sh + +# This scripts must be called the following way: +# vtpm-delete + +dir=$(dirname "$0") +. "$dir/vtpm-common.sh" + +vtpm_delete_instance $1 diff --git a/xen-unstable.hg/tools/examples/vtpm-hotplug-common.sh b/xen-unstable.hg/tools/examples/vtpm-hotplug-common.sh new file mode 100644 index 0000000000..9fd35e7402 --- /dev/null +++ b/xen-unstable.hg/tools/examples/vtpm-hotplug-common.sh @@ -0,0 +1,35 @@ +# +# Copyright (c) 2005 IBM Corporation +# Copyright (c) 2005 XenSource Ltd. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of version 2.1 of the GNU Lesser General Public +# License as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +dir=$(dirname "$0") +. "$dir/xen-hotplug-common.sh" + +findCommand "$@" +if [ "$command" != "online" ] && + [ "$command" != "offline" ] && + [ "$command" != "add" ] && + [ "$command" != "remove" ] +then + log err "Invalid command: $command" + exit 1 +fi + + +XENBUS_PATH="${XENBUS_PATH:?}" + +. "$dir/vtpm-common.sh" diff --git a/xen-unstable.hg/tools/xm-test/tests/vtpm/01_vtpm-list_pos.py b/xen-unstable.hg/tools/xm-test/tests/vtpm/01_vtpm-list_pos.py new file mode 100644 index 0000000000..00d5b3f8b2 --- /dev/null +++ b/xen-unstable.hg/tools/xm-test/tests/vtpm/01_vtpm-list_pos.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +# Copyright (C) International Business Machines Corp., 2006 +# Author: Stefan Berger